using UnityEngine;
using Simulation.Framework;
using UnityEngine.Events;


namespace Easy.Game2d
{
    public class AppPortal : MonoBehaviour
    {

        private void Awake()
        {
 
            InitLog();
            InitResources();
            InitTimerManager();
            InitDatas(() => {
                InitUserCache();
                InitAudio();
                InitUI();
                InitScene();
                InitStep();

            });
            DontDestroyOnLoad(gameObject);
        }

        private void OnDestroy()
        {
            ClearStep();
            ClearScene();
            ClearUI();
            ClearAudio();
            ClearTimerManager();
            ClearUserCache();
            ClearDatas();
            ClearResources();
            ClearLog();
      
        }

        #region log

        private void InitLog()
        {
            Log.Init();
        }

        private void ClearLog()
        {
            Log.Clear();
        }

        #endregion

        #region scene

        private void InitScene()
        {
            SingleBehavior<SceneManager>.getInstance().Init();   
        }

        private void ClearScene()
        {
            SingleBehavior<SceneManager>.Destroy();
        }

        #endregion

        #region data

        private void InitDatas(UnityAction unityAction)
        {
            DataManager.Init();
            DataManager.LoadData(() => {
                // Log.isClosed = DataManager.config.isCloseDebug;
                //Log.isDraw = DataManager.config.isDrawDebug;
                //Time.timeScale = DataManager.config.timeScale;
                unityAction?.Invoke();
            });
        }

        private void ClearDatas()
        {
            DataManager.Clear();
        }

        #endregion

        #region  resources

        private void InitResources()
        {
            //SingleBehavior<ResManager>.getInstance().Init();
        }

        private void ClearResources()
        {
            //SingleBehavior<ResManager>.getInstance().Clear();

        }

        #endregion

        #region timer

        private void InitTimerManager()
        {
            SingleBehavior<TimerManager>.getInstance().Init();
        }

        private void ClearTimerManager()
        {
            SingleBehavior<TimerManager>.Destroy();
        }

        #endregion

        #region UI

        private void InitUI()
        {
            SingleBehavior<PanelManager>.getInstance().Init();
        }

        private void ClearUI()
        {
            SingleBehavior<PanelManager>.Destroy();
        }

        #endregion

        #region audio

        private void InitAudio()
        {

            SingleBehavior<AudioManager>.getInstance().Init();

        }

        private void ClearAudio()
        {
            SingleBehavior<AudioManager>.Destroy();
        }

        #endregion



        #region step

        private void InitStep()
        {

            SingleBehavior<StepManager>.getInstance().AddStep(StepName.stepLogin);

            SingleBehavior<StepManager>.getInstance().AddStep(StepName.stepBoss);

            SingleBehavior<StepManager>.getInstance().AddStep(StepName.stepMonster);


            SingleBehavior<StepManager>.getInstance().PlayStep(StepName.stepLogin);

        }

        private void ClearStep()
        {
            SingleBehavior<StepManager>.Destroy();
        }

        #endregion

        #region user data

        private void InitUserCache()
        {
             
        }

        private void ClearUserCache()
        {
            
        }

        #endregion



    }
}
